package vip.xiaonuo.issued.controller.iot;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.pojo.httpclient.ReturnJson;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.util.OkHttpUtils;
import vip.xiaonuo.facedoor.iservice.IFaceDoorDtoService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.issued.controller.iot.vo.DeviceIdAndTypeVo;

import java.util.Map;

@RestController
@Api(value = "设备对外接口文档")
public class DeviceIotApi {


    @Resource
    private DeviceBaseService deviceBaseService;


    @ApiOperation(value = "通过序列化换取设备id", tags = "门禁设备-通过序列化换取设备id")
    @ApiImplicitParams({@ApiImplicitParam(name = "serialNumber", value = "设备序列号", dataType = "java.lang.String")})
    @PostMapping("/deviceIot/getDeviceIdBySerialNumber")
    public ResponseData getDeviceIdBySerialNumber(@RequestParam(value = "serialNumber", required = true) String serialNumber) {
        DeviceBase one = deviceBaseService.getOne(new LambdaQueryWrapper<DeviceBase>().eq(DeviceBase::getRegistrationCode, serialNumber));
        if (one == null) {
            throw new ServiceException(404, "不存在该设备！");
        }
        return ResponseData.success(new DeviceIdAndTypeVo().setDeviceId(one.getId()).setDeviceType("http"));
    }


    @ApiOperation(value = "发送获取操作日志消息", tags = "发送获取操作日志消息")
    @ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备ID", dataType = "java.lang.Long")})
    @PostMapping("/deviceIot/getDeviceOperateFile")
    public ResponseData getDeviceOperateFile(@RequestBody GetDeviceOperateFileParam param) {
        DeviceBase one = deviceBaseService.getById(param.getDeviceId());

        if (one == null) {
            throw new ServiceException(404, "不存在该设备！");
        }
        Map<String, Object> baseMap = SnowIdGen.getBaseMap();
        baseMap.put("deviceId", param.getDeviceId());
        baseMap.put(CMD.CMD, CMD.UPLOAD_LOG);
        String uri = IFaceDoorDtoService.getBaseParam(one.getCabinetIp(), Integer.parseInt(one.getCabinetPort()));
        try {
            OkHttpUtils.requestDevice(OkHttpUtils.ReqType.GET, uri.concat("/upload_log"), baseMap, ReturnJson.class);
        } catch (Exception e) {
            throw new ServiceException(500, "获取日志失败" + "【{" + ExceptionUtil.getMessage(e) + "}】");
        }
        return ResponseData.success();
    }


    @ApiOperation(value = "发送升级app消息", tags = "发送升级app消息")
    @ApiImplicitParams({@ApiImplicitParam(name = "deviceId", value = "设备ID", dataType = "java.lang.Long")})
    @PostMapping("/deviceIot/updateApk")
    public ResponseData updateApk(@RequestBody GetDeviceOperateFileParam param) {
        DeviceBase one = deviceBaseService.getById(param.getDeviceId());

        if (one == null) {
            throw new ServiceException(404, "不存在该设备！");
        }
        Map<String, Object> baseMap = SnowIdGen.getBaseMap();
        baseMap.put("deviceId", param.getDeviceId());
        baseMap.put(CMD.CMD, CMD.UPDATE_APK);
        String uri = IFaceDoorDtoService.getBaseParam(one.getCabinetIp(), Integer.parseInt(one.getCabinetPort()));
        try {
            OkHttpUtils.requestDevice(OkHttpUtils.ReqType.GET, uri.concat("/update_apk"), baseMap, ReturnJson.class);
        } catch (Exception e) {
            throw new ServiceException(500, "获取日志失败" + "【{" + ExceptionUtil.getMessage(e) + "}】");
        }
        return ResponseData.success();
    }
}
